package multidraw.tools;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;

import multidraw.model.DrawingCanvasModel;
import multidraw.model.OvalShape;
import multidraw.model.Shape;
import multidraw.model.TriangleShape;
import multidraw.model.TwoEndShape;
import multidraw.model.commands.AddShapeCommand;
import multidraw.model.commands.WrapperCommand;

public class TriangleTool extends Tool {
	//protected DrawingCanvasModel myModel;
	protected Point startingMousePosition;
	protected Point currentMousePosition;
	protected TriangleShape shape;
	
	
	public TriangleTool(DrawingCanvasModel myModel) {
		this.myModel = myModel;		
	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		//create new temp shape
		shape = createShape(e.getPoint(), e.getPoint(), 
				myModel.getPenColor(), myModel.getNextID());
		System.out.println("triangle shape: " + shape);
		startingMousePosition = e.getPoint();
		currentMousePosition = e.getPoint();
		//update content
		myModel.notifyObservers();
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		//add shape to the model and return to the normal state
		//myModel.addShape((Shape)shape);
		AddShapeCommand cmd = new AddShapeCommand(shape, myModel.getRevisionID() + 1);
		try {
			WrapperCommand wrapCmd = new WrapperCommand(cmd);
			myModel.addCommand(wrapCmd);
		} catch (Exception e1) {
			System.out.println(e1);
		}
		shape = null;
		//notify observers
		myModel.notifyObservers();
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	//Mouse motion listener's methods
	
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		//update the shape information,
		//then update the model
		currentMousePosition = e.getPoint();
		if (shape != null) {
			shape.updateShapeInfo(startingMousePosition, currentMousePosition);
		}
		
		//update model's views
		myModel.notifyObservers();
	}

	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public Shape getShape() {
		return (Shape)shape;
	}
	
	protected TriangleShape createShape(Point p1, Point p2, Color c, long id) {
		//construct the rectangle from the points
		double shapeX; //left X, smallest
		double shapeY; //top Y, smallest
		double width;
		double height;
		
		System.out.println("creating triangle shape ...");
		
		if (p1.getX() <= p2.getX()) {
			shapeX = p1.getX();
			width = p2.getX() - p1.getX() + 1; 
		} else {
			shapeX = p2.getX();
			width = p1.getX() - p2.getX() + 1;
		}
		
		if (p1.getY() <= p2.getY()) {
			shapeY = p1.getY();
			height = p2.getY() - p1.getY() + 1;			
		} else {
			shapeY = p2.getY();
			height = p1.getY() - p2.getY() + 1;
		}
		
		return new TriangleShape(shapeX, shapeY, width, height, c, id);
		//return null;
	}
}
